package RequestHandle;

import entity.RpcRequest;

import java.lang.reflect.Method;

public class RequestHandler {
    public Object handle(Object service, RpcRequest request) {
        Object res = null;

        try{
            res = invokeTargetMethod(service, request);
        }catch (Exception e){
            System.out.println("调用方法时有问题！！");
        }
        return res;
    }

    private Object invokeTargetMethod(Object service, RpcRequest request) {
        Method method = null;
        try{
            method = service.getClass().getMethod(request.getMethodName(), request.getParamTypes());
            return method.invoke(service, request.getParameters());
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("没找到要调用的方法!!!");
            return null;
        }
    }
}
